<?php
/*
Plugin Name: TargetShooting 
Plugin URI: http://www.thegentootux.com
Description: A wordpress plugin designed for managing target shooting leagues
Version: 0.0.1
Author: Jacob Langley (jacob.langley@gmail.com)
Author URI: http://www.thegentootux.com
*/
require_once($ts_path . 'tables.php');

// Get the path relative to the plugins directory in which FWP is stored
preg_match (
	'|/wp-content/plugins/(.+)$|',
	dirname(__FILE__),
	$ref
);

if (isset($ref[1])) :
	$ts_path = $ref[1];
else : // Something went wrong. Let's just guess.
	$ts_path = 'wp-targetshooting';
endif;

function ts_activate()
{
	require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
	global $wpdb;
	global $ts_dbversion;
	global $table_ranges;
	if($wpdb->get_var("show tables like $table_ranges") != $table_ranges)
	{
		global $table_ranges_create;
		dbDelta($table_ranges_create);
	}
	global $table_shooters;
	if($wpdb->get_var("show tables like $table_shooters") != $table_shooters)
	{
		global $table_shooters_create;
		dbDelta($table_shooters_create);
	}
	global $table_eventtypes;
	if($wpdb->get_var("show tables like $table_eventtypes") != $table_eventtypes)
	{
		global $table_eventtypes_create;
		dbDelta($table_eventtypes_create);
	}
	global $table_shoots;
	if($wpdb->get_var("show tables like $table_shoots") != $table_eventtypes)
	{
		global $table_shoots_create;
		dbDelta($table_shoots_create);
	}
	global $table_events;
	if($wpdb->get_var("show tables like $table_events") != $table_eventtypes)
	{
		global $table_events_create;
		dbDelta($table_events_create);
	}
	global $table_rounds;
	if($wpdb->get_var("show tables lke $table_rounds") != $table_rounds)
	{
		global $table_rounds_create;
		dbDelta($table_rounds_create);
	}
	add_option("ts_dbversion", $ts_dbversion);
}
function ts_deactivate()
{
}
register_activation_hook(__FILE__, 'ts_activate');
register_deactivation_hook(__FILE__, 'ts_deactivate');

// Hook for adding admin menus
add_action('admin_menu', 'ts_add_admin_pages');


if (!function_exists('add_filter'))
	die ("Hello World!");

if (!function_exists('get_themes'))
	require_once (ABSPATH."wp-admin/includes/theme.php");

add_filter('the_content', 'ts_content_filter');

function ts_add_admin_pages() 
{
	global $ts_path;
	
	add_menu_page("WPTargetShooting", "TargetShooting", 10, __FILE__, "ts_toplevel_admin_page");
	add_submenu_page(__FILE__, 'WPTargetShooting', 'Scores',   8, $ts_path . "/admin_scores.php");
	add_submenu_page(__FILE__, 'WPTargetShooting', 'Shoots',   8, $ts_path . "/admin_shoots.php");
	add_submenu_page(__FILE__, 'WPTargetShooting', 'Shooters', 8, $ts_path . "/admin_shooters.php");
	add_submenu_page(__FILE__, 'WPTargetShooting', 'Ranges',   8, $ts_path . "/admin_ranges.php");
}

function ts_toplevel_admin_page()
{
?>
	<div class="wrap">
	<h2>Most Used Stuff</h2>
        Eventually this will be shortcuts to most used functions<br />
    </div>
<?php
}

function ts_content_filter($content)
{
	$regex = "/\[ts\-result shootid=([0-9]*) sortid=(score|lastname|classscore|clubscore|concurrentaverage)\]/ei";
	if(preg_match($regex, $content, $matches))
	{
		$content = str_replace($matches[0], event_results_condensed($matches[1],$matches[2]), $content);
	}
	return $content;
}


function shooter_avg($shootid, $shooterid)
{
	global $wpdb;
	$query=$wpdb->prepare("select AVG(BIT_COUNT($table_rounds.round1) + BIT_COUNT($table_rounds.round2) + BIT_COUNT($table_rounds.round3) + BIT_COUNT($table_rounds.round4)) as avg " .
					"from $table_rounds " .
					"join $table_events on $table_rounds.eventid=$table_events.eventid " . 
					"join $table_shoots on $table_events.shootid=$table_shoots.shootid " . 
					"where shooterid=%d " . 
					"and $table_shoots.date<(select $table_shoots.date from $table_shoots where shootid=%d order by date desc limit 5') ",
					$shooterid, $shootid);
	$shooteravg=$wpdb->get_var($query);
	if ($result_shooteravg === FALSE) {
	    	die($wpdb->print_error());
	}
	return $shooteravg;
}

function event_results_condensed($shootid,$sortid)
{
	global $wpdb;
	global $table_events, $table_eventtypes, $table_shoots;
	$str="";
	/* Get the proper event information for the shoot */
	$query = $wpdb->prepare("select $table_events.eventid, $table_eventtypes.name as eventname, $table_shoots.name as shootname, $table_shoots.date " . 
								 "from $table_events " . 
								 "join $table_eventtypes on $table_events.eventtypeid=$table_eventtypes.eventtypeid " .
								 "join $table_shoots on $table_events.shootid=$table_shoots.shootid " .
								 "where $table_events.shootid=%d", $shootid);
	$shootevents = $wpdb->get_results($query);
	if($shootevents === FALSE) 
	{
		die($wpdb->print_error());
	}
	
	/* Generate the proper result tables*/
	foreach($shootevents as $shootevent)
	{
		$str .= "<h3>" . $shootevent->eventname . ", sorted by " . $sortid . "</h3>\n";
		
		if($sortid == "lastname")
		{
			/*
			$query_eventresults = sprintf("select $table_shooters.firstname, $table_shooters.lastname, " .
						      "$table_rounds.round1, $table_rounds.round2, $table_rounds.round3, $table_rounds.round4, " .
						      "BIT_COUNT($table_rounds.round1) + BIT_COUNT($table_rounds.round2) + BIT_COUNT($table_rounds.round3) + BIT_COUNT($table_rounds.round4) as score " . 
						      "from $table_rounds " . 
						      "join $table_shooters on $table_rounds.shooterid=$table_shooters.shooterid " . 
						      "where $table_rounds.eventid='%s' order by score desc, $table_rounds.round1 desc, $table_rounds.round2 desc, $table_rounds.round3 desc, $table_rounds.round4 desc",
						      $shootevent->eventid);*/
		}
		else
		{
			global $table_shooters, $table_rounds;
			$query = $wpdb->prepare("select $table_shooters.shooterid, $table_shooters.firstname, $table_shooters.lastname, " . 
					      	      "BIT_COUNT($table_rounds.round1) + BIT_COUNT($table_rounds.round2) + BIT_COUNT($table_rounds.round3) + BIT_COUNT($table_rounds.round4) as score " . 
						      "from $table_rounds " . 
						      "join $table_shooters on $table_rounds.shooterid=$table_shooters.shooterid " . 
						      "where $table_rounds.eventid=%d order by score desc, $table_rounds.round1 desc, $table_rounds.round2 desc, $table_rounds.round3 desc, $table_rounds.round4 desc",
						      $shootevent->eventid);
			$eventresults = $wpdb->get_results($query);
			if ($eventresults === FALSE) {
			        die($wpdb->print_error());
			}
			$str .= "<table border=1>";
			foreach($eventresults as $eventresult)
			{
				$str .= "<tr><td>$eventresult->lastname, $eventresult->firstname</td><td>$eventresult->score</td></tr>\n";
			}
			$str .= "</table>";
		}
	}
	return $str;
}

function user_results_condensed()
{
}

function user_results_full()
{
	$str="";
	$str .= "<table border=1>";
	$str .= "<tr>";
	$str .= "<th colspan=\"4\">1</th>";
	$str .= "<th colspan=\"4\">2</th>";
	$str .= "<th colspan=\"2\">3</th>";
	$str .= "<th colspan=\"2\">4</th>";
	$str .= "<th colspan=\"2\">5</th>";
	$str .= "<th colspan=\"4\">6</th>";
	$str .= "<th colspan=\"4\">7</th>";
	$str .= "<th colspan=\"2\">8</th>";
	$str .= "<th>O</th>";
	$str .= "</tr>";
	$str .= "<tr>";
	$str .= "<td>h</td>";
	$str .= "<td>l</td>";
	$str .= "<td>h</td>";
	$str .= "<td>l</td>";
	$str .= "<td>h</td>";
	$str .= "<td>l</td>";
	$str .= "<td>h</td>";
	$str .= "<td>l</td>";
	$str .= "<td>h</td>";
	$str .= "<td>l</td>";
	$str .= "<td>h</td>";
	$str .= "<td>l</td>";
	$str .= "<td>h</td>";
	$str .= "<td>l</td>";
	$str .= "<td>h</td>";
	$str .= "<td>l</td>";
	$str .= "<td>l</td>";
	$str .= "<td>h</td>";
	$str .= "<td>h</td>";
	$str .= "<td>l</td>";
	$str .= "<td>l</td>";
	$str .= "<td>h</td>";
	$str .= "<td>h</td>";
	$str .= "<td>l</td>";
	$str .= "<td></td>";
	$str .= "</tr>";
	$str .= "<tr>";

	for($i=1;$i<26;$i++)
	{
		$str .= "<td><input type=\"checkbox\" name=\"" . $i . "\" value=\"1\" disabled=\"disabled\"/></td>";
	}
	$str .= "</table>";	
	return $str;
}

?>
